package com.feng.test.json;

import java.io.IOException;
import java.time.Instant;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONObject;

/**
 * @author HuangSuip
 */
public class JSONTEST {

    static String json = "{\"props\":{\"background\":{\"image\":\"\",\"color\":\"#fff\",\"size\":\"contain\"}}}";
    private static JSONObject jsonObject;

    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        jsonObject = objectMapper.readValue(json, JSONObject.class);
        System.out.println(jsonObject);
        JSONObject jsonObject2 = jsonObject.getJSONObject("props");
        System.out.println(jsonObject2);
        JSONObject background = jsonObject2.getJSONObject("background");
        System.out.println(background.get("image").getClass());
        System.out.println(jsonObject2.getJSONObject("faf").isEmpty());
        System.out.println(jsonObject2.getJSONObject("image").getClass());

    }

    private static void test111(java.lang.Object o2) {
        String s = (String) o2;
        System.out.println(s);
    }
}

class TestJsonDate {
    public static void main(String[] args) {
        Instant now = Instant.now();

        System.out.println(JSON.toJSONString(now));
    }
}

class TestJsonByte {

    public static void main(String[] args) throws Exception {
        JsonByte jb = new JsonByte();
        jb.setB(1);
        jb.setS("json");
        String s = JSON.toJSONString(jb);
        System.out.println(s);
        String json = "{\"b\":1,\"s\":\"json\"}";

        JsonByte jsonByte = JSON.parseObject(json, JsonByte.class);
        System.out.println(jsonByte.getB());

        ObjectMapper om = new ObjectMapper();
        JsonByte jsonByte1 = om.readValue(json, JsonByte.class);
        System.out.println(jsonByte1.getB());

    }
}

class JsonByte {
    private Number b;
    private String s;

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }

    public Number getB() {
        return b;
    }

    public void setB(Number b) {
        this.b = b;
    }
}


class  JsonArray{
    public static void main(String[] args) {
        Long [] ids = {1L, 2L ,3L};
        String s = JSON.toJSONString(ids);
        System.out.println(s);
    }
}
class StaticObject{

    public static void main(String[] args) {
        Huang h = new Huang();
        h.setName("Huang");
        h.age = 10;
        String s = JSON.toJSONString(h);
        System.out.println(s);
    }

    static class Huang{

        String name;
        int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}

